package com.premize.grco.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.premize.grco.dataaccess.sessionFactory.HibernateSessionFactory;
import com.premize.grco.movil.GrcoPUsuario;


/**
 * A data access object (DAO) providing persistence and search support for
 * GrcoPUsuario entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class GrcoPUsuarioDAO implements IGrcoPUsuarioDAO {
    private static final Log log = LogFactory.getLog(GrcoPUsuarioDAO.class);

    // property constants
    //public static final Long  CONSUSUARIO = "consUsuario";
    public static final String CONSUSUARIO = "consUsuario";

    //public static final Date  FECHACREACION = "fechaCreacion";
    public static final String FECHACREACION = "fechaCreacion";

    //public static final Date  FECHAMODIFICA = "fechaModifica";
    public static final String FECHAMODIFICA = "fechaModifica";

    //public static final String  PASSWORD = "password";
    public static final String PASSWORD = "password";

    //public static final Long  ROL = "rol";
    public static final String ROL = "rol";

    //public static final String  USUARIO = "usuario";
    public static final String USUARIO = "usuario";

    //public static final String  USUARIOCREACION = "usuarioCreacion";
    public static final String USUARIOCREACION = "usuarioCreacion";

    //public static final String  USUARIOMODIFICA = "usuarioModifica";
    public static final String USUARIOMODIFICA = "usuarioModifica";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            GrcoPUsuario Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(GrcoPUsuario instance) {
        log.debug("saving GrcoPUsuario instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            GrcoPUsuario Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(GrcoPUsuario instance) {
        log.debug("deleting GrcoPUsuario instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            GrcoPUsuario Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(GrcoPUsuario instance) {
        log.debug("updating GrcoPUsuario instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public GrcoPUsuario findById(Long id) {
        log.debug("finding GrcoPUsuario instance with id: " + id);

        try {
            GrcoPUsuario instance = (GrcoPUsuario) getSession()
                                                       .get(GrcoPUsuario.class,
                    id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding GrcoPUsuario failed", re);
            throw re;
        }
    }

    public List<GrcoPUsuario> findByExample(GrcoPUsuario instance) {
        log.debug("finding GrcoPUsuario instance by example");

        try {
            List results = getSession().createCriteria("GrcoPUsuario")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  GrcoPUsuario entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< GrcoPUsuario> found by query
        */
    public List<GrcoPUsuario> findByProperty(String propertyName, Object value) {
        log.debug("finding GrcoPUsuario instance with property: " +
            propertyName + ", value: " + value);

        try {
            String queryString = "from GrcoPUsuario as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<GrcoPUsuario> findByConsUsuario(Object consUsuario) {
        return findByProperty(CONSUSUARIO, consUsuario);
    }

    public List<GrcoPUsuario> findByFechaCreacion(Object fechaCreacion) {
        return findByProperty(FECHACREACION, fechaCreacion);
    }

    public List<GrcoPUsuario> findByFechaModifica(Object fechaModifica) {
        return findByProperty(FECHAMODIFICA, fechaModifica);
    }

    public List<GrcoPUsuario> findByPassword(Object password) {
        return findByProperty(PASSWORD, password);
    }

    public List<GrcoPUsuario> findByRol(Object rol) {
        return findByProperty(ROL, rol);
    }

    public List<GrcoPUsuario> findByUsuario(Object usuario) {
        return findByProperty(USUARIO, usuario);
    }

    public List<GrcoPUsuario> findByUsuarioCreacion(Object usuarioCreacion) {
        return findByProperty(USUARIOCREACION, usuarioCreacion);
    }

    public List<GrcoPUsuario> findByUsuarioModifica(Object usuarioModifica) {
        return findByProperty(USUARIOMODIFICA, usuarioModifica);
    }

    /**
    * Find all GrcoPUsuario entities.
    *
    * @return List<GrcoPUsuario> all GrcoPUsuario instances
    */
    public List<GrcoPUsuario> findAll() {
        log.debug("finding all GrcoPUsuario instances");

        try {
            String queryString = "from GrcoPUsuario";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<GrcoPUsuario> findByCriteria(String whereCondition) {
        log.debug("finding GrcoPUsuario " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from GrcoPUsuario model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<GrcoPUsuario> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in GrcoPUsuario failed", re);
            throw re;
        }
    }

    public List<GrcoPUsuario> findPageGrcoPUsuario(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding GrcoPUsuario findPageGrcoPUsuario");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from GrcoPUsuario model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from GrcoPUsuario model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberGrcoPUsuario() {
        log.debug("finding GrcoPUsuario count");

        try {
            String queryString = "select count(*) from GrcoPUsuario model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
